<template>
	<view class="pages_intelligence" :style="{paddingTop: `${search.height+search.top+10}px`}">
		<myTabs :title="title" @goBlack="goBlack" :mybackgroundColor="'#f0f0f0'"></myTabs>
		<view class="pages_payment">
			<view class="pages_paymentTopBox">
				<view class="pages_paymentPrice">
					<span>￥</span>
					<span>{{detail.payPrice}}</span>
					<span v-if="buyType==30">/年</span>
				</view>
				<view class="pages_paymentText">
					应付费用
				</view>
			</view>
			<view class="pages_paymentConten">
				<view class="pages_paymentItem">
					<view>订单用处</view>
					<view v-if="buyType!=30">解锁「{{informationTitle}}」费用</view>
					<view v-else>{{ff_locationName()}} {{location.name}}全品类大数据1年 费用</view>
				</view>
				<view class="pages_paymentItem">
					<view>订单类型</view>
					<view class="pages_paymentItemWx">
						<image src="/static/img/wxIcon.png"></image>
						<span>微信支付</span>
					</view>
				</view>
			</view>
			<view @click="payBuy" class="pages_paymentBottom">
				立即支付
			</view>

			<!-- 支付成功 -->
			<u-overlay :show="winShow" @click="winShow = false">
				<view class="winwarpBox">
					<view class="winRect" @tap.stop>
						<image src="/static/img/winIcon.png"></image>
						<span>支付成功</span>
					</view>
				</view>
			</u-overlay>
		</view>

	</view>
</template>

<script>
	import myTabs from "@/components/myTabs/index.vue"
	export default {
		components: {
			myTabs
		},
		data() {
			return {
				title: '微信支付',
				detail: null,
				// 订单类型【10=情报 20=我要学 30=社区 40=我要学-菜品】
				buyType: 0,
				informationId: "",
				studyId: '',
				priceId: '',
				communityId: '',
				informationTitle: '主题',
				winShow: false,
				url: null,
				isComCode: false
			}
		},
		onLoad(options) {
			// #ifndef H5
			let search = uni.getMenuButtonBoundingClientRect()
			this.$store.commit('updataSearch', search);
			// #endif
			this.buyType = options.buyType;
			this.informationTitle = options.informationTitle;
			if (options.informationId) {
				this.informationId = options.informationId;
			}
			if (options.studyId) {
				this.studyId = options.studyId;
			}
			if (options.communityId && options.priceId) {
				this.communityId = options.communityId;
				this.priceId = options.priceId;
				console.log('options',options)
				this.url = options.url;
				this.isComCode =options.isComCode
			}
			this.creatOrder();
		},
		computed: {
			search() {
				return this.$store.state.search;
			},
			location() {
				return this.$store.state.location;
			},
			user() {
				return this.$store.state.user;
			},

		},
		methods: {
			ff_locationName(){
				if(this.user){
					let isPass = false;
					this.user.communityList.forEach(item=>{
						if(item.communityName == this.location.name){
							isPass=true;
						}
					})
					if(isPass){
						return '续费'
					}else{
						return '解锁'
					}
				}else{
					return '解锁'
				}
			},
			goBlack() {
				uni.navigateBack();
			},
			creatOrder() {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				let req = {
					buyType: this.buyType,
					informationId: this.informationId, //情报主键，订单类型为情报时		
					communityId: this.communityId, //社区主键，订单类型为社区时	
					priceId: this.priceId, //价格主键，订单类型为社区时
					studyId: this.studyId, //学习主键，订单类型为我要学时

				}
				this.$api.orderCreate(req).then(res => {
					if (res.code == 200) {
						uni.hideLoading();
						this.detail = res.data;
					}
				})
			},
			payBuy() {
				this.$api.wxPay({
					orderNo: this.detail.orderNo
				}).then(res => {
					if (res.code == 200) {
						uni.showLoading({
							title: '加载中',
							mask: true
						})
						let {
							result
						} = res.data;
						if (res.data.needPay) {
							uni.requestPayment({
								nonceStr: result.nonceStr,
								package: result.packageValue,
								paySign: result.paySign,
								timeStamp: result.timeStamp,
								signType: result.signType,
								success: threeRes => {
									console.log('支付成功', threeRes)
									this.orderWxQuery(this.detail.orderNo)
								},
								fail: threeRes => {
									console.log('支付失败', threeRes)
									this.orderWxQuery(this.detail.orderNo)
								}
							});
						} else {
							uni.showToast({
								icon: 'none',
								title: '订单已支付',
							})
						}

					}
				})
			},
			orderWxQuery(orderNo) {
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				this.$api.orderWxQuery({
					orderNo
				}).then(res => {
					if (res.code == 200) {

						if (res.data.isPay) {
							let pages = getCurrentPages();
							let prevPage = pages[pages.length - 2];
							let id = this.buyType == 10 ? this.informationId : this.buyType == 20 || this.buyType == 40 ? this.studyId :
								this.buyType == 30 ? this.communityId : '';
							uni.hideLoading();
							this.winShow = true;
							setTimeout(() => {
								this.winShow = false;
								prevPage.onShow(id);
							
								if(this.buyType==30){
									let location = uni.getStorageSync('location');
									
									if(this.url == '/pages/index/index'){
										this.$api.userLocation({
											city: location.name
										}).then(res => {
											if (res.code == 200) {
												console.log(`${this.url}, ${this.isComCode}`,this.buyType)
												this.$store.commit('updataUser',res.data.user);
												this.$store.commit('updatalocation',res.data.location)
												
												getApp().globalData.isComCode = this.isComCode;
												uni.switchTab({
													url: this.url
												})
											}
										})
										
									}else{
											console.log(`${this.url}, ${this.isComCode}`,this.buyType)
										uni.redirectTo({
											url: `${this.url}?isComCode=${this.isComCode}` 
										})
									}
									
								}else{
									uni.navigateBack();
								}
								
							}, 1000);
						} else {
							uni.showToast({
								title: '支付失败',
								mask: true,
								icon: 'none'
							});
							// 跳转订单页面

						}
					}
				})

			}
		}
	}
</script>
<style lang="scss" scoped>
	.pages_intelligence {
		background-color: #F0F0F0;
		min-height: 100vh;
		box-sizing: border-box;

		.pages_paymentBottom {
			width: 558rpx;
			height: 88rpx;
			line-height: 88rpx;
			background: #FFDA2D;
			border-radius: 24rpx;
			font-size: 28rpx;
			font-family: 蘋方-簡;
			font-weight: bold;
			color: #000000;
			text-align: center;
			margin: 56rpx auto;
		}

		.winwarpBox {
			display: flex;
			justify-content: center;
			align-items: center;
			width: 100%;
			height: 100vh;
		}

		.winRect {
			width: 242rpx;
			height: 242rpx;
			background: #fff;
			opacity: 1;
			border-radius: 32rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			image {
				width: 128rpx;
				height: 128rpx;
			}

			span {
				margin-top: 8rpx;
				display: inline-block;
				height: 34rpx;
				font-size: 24rpx;
				font-family: 蘋方-簡;
				font-weight: normal;
				line-height: 34rpx;
				color: #333333;
			}
		}

		.pages_paymentConten {
			background-color: #fff;
			border-radius: 24rpx;
			padding: 0 24rpx;

		}

		.pages_paymentItem {
			padding: 32rpx 0;
			display: flex;
			justify-content: space-between;
			align-items: center;

			view:nth-child(1) {
				height: 40rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 400;
				line-height: 40rpx;
				color: #999999;
				flex: 0 0 150rpx;
				margin-right: 20rpx;
			}

			view:nth-child(2) {
				flex: 1;
				height: 40rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: bold;
				line-height: 40rpx;
				color: #333333;
				overflow: hidden;
				white-space: nowrap;
				/*文字超出宽度则显示ellipsis省略号*/
				text-overflow: ellipsis;
				text-align: right;
			}
		}

		.pages_paymentItem:not(:last-child) {
			border-bottom: 1rpx solid #ececec;
		}

		.pages_paymentTopBox {
			margin: 64rpx 0 32rpx 0;
		}

		.pages_paymentPrice {
			display: flex;
			justify-content: center;

			span:nth-child(1) {
				height: 48rpx;
				font-size: 42rpx;
				font-family: D-DIN;
				font-weight: normal;
				line-height: 76rpx;
			}

			span:nth-child(2) {
				height: 62rpx;
				font-size: 62rpx;
				font-family: D-DIN;
				font-weight: normal;
				line-height: 62rpx;
				color: #000000;
			}
			span:nth-child(3) {
				    height: 48rpx;
				    font-size: 42rpx;
				    font-family: D-DIN;
				    font-weight: normal;
				    line-height: 70rpx;
				    font-weight: bold;
			}
		}

		.pages_paymentText {
			height: 34rpx;
			font-size: 24rpx;
			font-family: PingFang SC;
			font-weight: 400;
			line-height: 34rpx;
			color: #999999;
			text-align: center;
			margin-top: 8rpx;
		}

		.pages_payment {
			padding: 0 32rpx;
			width: 100%;
			box-sizing: border-box;
		}

		.pages_paymentItemWx {
			display: flex;
			align-items: center;
			justify-content: flex-end;

			image {
				width: 40rpx;
				height: 40rpx;
			}

			span {
				display: inline-block;
				height: 40rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: bold;
				line-height: 40rpx;
				color: #333333;
				margin-left: 8rpx;
			}

		}
	}
</style>
